using System;
using System.Linq;
using Microsoft.Xna.Framework;
using ThinkDotNetBotApiCommon;
using ThinkDotNetBotApiCommon.Helpers;
using ThinkDotNetBotApiCommon.Weapons;

namespace SampleRobot1
{
    public class SampleBot1:BotBase
    {
        public SampleBot1(IBotController controller):base(controller)
        {
            
        }

        private BotMetadata _botMetadata;
        public override BotMetadata Metadata
        {
            get
            {
                if(_botMetadata==null)
                {
                    _botMetadata = new BotMetadata {Name = "Sample bot 1"};
                }
                return _botMetadata;
            }
        }

        private Random _random=new Random();
        private IScanableItem _detectedObject;
        private const float FireMaxDist = 100;
        private const float ScanDistance = 10000;

        public override void Update(GameTime gameTime)
        {
            if (_detectedObject!=null&&IsEnemyInRange(_detectedObject))           
                BotController.Stop();
            if(BotController.IsMoving)
                return;

            IScanableItem[] scanedItems = BotController.Scan(ScanDistance);
            if(scanedItems.Length!=0)
            {
                _detectedObject = scanedItems.Where(s => s is BotInfo).FirstOrDefault();
                BotInfo enemyBotInfo = (BotInfo)_detectedObject;

                if(_detectedObject!=null)
                {
                    if (!IsEnemyInRange(enemyBotInfo))
                    {
                        BotController.Move(enemyBotInfo.Translation);
                        return;
                    }
                    else
                    {
                        BotController.Stop();
                        _detectedObject = null;
                    }
                }
                if (BotController.IsTurretReady)
                {                                        
                    Vector3 enemyVec = (enemyBotInfo.Translation - BotController.Translation);
                    var enemyVec2 = new Vector2(enemyVec.X, enemyVec.Z);

                    double angle = MathHelperEx.GetAngleBetweenVectors(Vector2.UnitY, enemyVec2);

                    BotController.MoveTurret((float) angle - BotController.RotationY);

                }

                if(BotController.IsCannonReady)
                   BotController.MoveCannon(_random.Next(-1,2)*MathHelper.Pi/10f);
            }
            
            if(_random.Next(5)==1)
                BotController.ChangeWeapon(CannonWeaponType.Rocket);                
            else
                BotController.ChangeWeapon(CannonWeaponType.Plasma);

            BotController.LaunchAmmo();      
        }
        private bool IsEnemyInRange(IScanableItem enemy)
        {
            return Vector3.Distance(enemy.Translation, BotController.Translation) <= FireMaxDist;
        }
    }
}
